home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyo (Python 2.5) from mapping import dictadd from util.lrucache import lru_cache import HTMLParser import StringIO import math import re import string import sys import traceback try: _ except NameError: _ = lambda x: x def strftime_u(timeobj, fmt): if isinstance(fmt, unicode): fmt = fmt.encode('utf-8') return timeobj.strftime(fmt).decode('locale') def strfileobj(writefunc): s = StringIO() writefunc(s) return s.getvalue() def strincrement(s): return str(int(s) + 1) def tuple2hex(t): return ''.join((lambda .0: for c in .0: '%02x' % c)(t[:3])) def format_xhtml(s, f): s = s.replace('\n', '<br />') font_attrs = None(None, [ None, None, filter, (lambda e: bool(e)) if f.bold else '' if f.italic else '' if f.size is not None else '' if f.face is not None else '']) style_elems = None(None, [ None, None, filter, (lambda s: bool(s)) if f.foregroundcolor is not None else '' if f.backgroundcolor not in (None, (255, 255, 255, 255)) else '' if f.underline else '' if font_attrs else '']) if style_elems: span_style = '; '.join(style_elems) return '<span style="%s;">%s</span>' % (span_style, s) else: return s fontsizes = ((lambda x: x < 8), (lambda x: x == 8), (lambda x: x in (9, 10)), (lambda x: x in (11, 12, 13)), (lambda x: x in (14, 15, 16)), (lambda x: x in (17, 18, 19)), (lambda x: x > 19)) def Point2HTMLSize(n): for i, f in enumerate(fontsizes): if f(n): return i + 1 continue class StrippingParser(HTMLParser.HTMLParser): from htmlentitydefs import entitydefs def __init__(self, valid_tags = ()): HTMLParser.HTMLParser.__init__(self) self.valid_tags = valid_tags self.result = '' self.endTagList = [] def handle_data(self, data): if data: self.result = self.result + data def handle_starttag(self, tag, attrs): if tag == 'br': self.result += '\n' def handle_charref(self, name): self.result += chr(int(name)) def handle_entityref(self, name): self.result += self.entitydefs.get(name, '') def unknown_starttag(self, tag, attrs): if tag in self.valid_tags: self.result = self.result + '<' + tag for k, v in attrs: if string.lower(k[0:2]) != 'on' and string.lower(v[0:10]) != 'javascript': self.result = '%s %s="%s"' % (self.result, k, v) continue endTag = '</%s>' % tag self.endTagList.insert(0, endTag) self.result = self.result + '>' def unknown_endtag(self, tag): if tag in self.valid_tags: self.result = '%s</%s>' % (self.result, tag) remTag = '</%s>' % tag self.endTagList.remove(remTag) def cleanup(self): for j in range(len(self.endTagList)): self.result = self.result + self.endTagList[j] parser = StrippingParser() def scrape_clean(text): def fixup(m): text = m.group(0) if text[:1] == '<': return '' if text[:2] == '': try: if text[:3] == '': return unichr(int(text[3:-1], 16)) else: return unichr(int(text[2:-1])) except ValueError: pass except: None<EXCEPTION MATCH>ValueError None<EXCEPTION MATCH>ValueError if text[:1] == '&': import htmlentitydefs entity = htmlentitydefs.entitydefs.get(text[1:-1]) if entity: if entity[:2] == '': try: return unichr(int(entity[2:-1])) except ValueError: pass except: None<EXCEPTION MATCH>ValueError None<EXCEPTION MATCH>ValueError return unicode(entity, 'iso-8859-1') return text return re.sub('(?s)<[^>]*>|?\\w+;', fixup, text) def strip_html(s, valid_tags = ()): parser.valid_tags = valid_tags parser.feed(s) parser.close() parser.cleanup() result = parser.result parser.result = '' return result def strip_html2(s): if not s: return s BeautifulSoup = BeautifulSoup import util.BeautifulSoup soup = BeautifulSoup(s.replace('<br>', '\n').replace('<br/>', '\n').replace('<br />', '\n')) return ''.join((lambda .0: for e in .0: if isinstance(e, unicode): econtinue)(soup.recursiveChildGenerator())) strip_html2 = lru_cache(80)(strip_html2) def strip_html_and_tags(s, invalid_tags): if not s: return s BeautifulSoup = BeautifulSoup import util.BeautifulSoup soup = BeautifulSoup(s.replace('<br>', '\n').replace('<br/>', '\n').replace('<br />', '\n')) for tag in invalid_tags: for result in soup.findAll(name = tag): result.replaceWith('') return ''.join((lambda .0: for e in .0: if isinstance(e, unicode): econtinue)(soup.recursiveChildGenerator())) def srv_str_to_tuple(address, default_port): if address.find(':') != -1: (host, port) = address.split(':') port = int(port) else: host = address port = default_port return (host, port) def get_between(s, start_str, end_str): start_i = s.find(start_str) end_i = s.find(end_str, start_i) if (start_i > end_i or start_i == -1) and end_i == -1: return None else: return s[start_i + len(start_str):end_i] def strlist(s): lines = s.split('\n') for y, line in enumerate(lines): i = line.find('#') if i != -1: lines[y] = line[:i] continue return '\n'.join(lines).split() _curlymatcher = re.compile('\\$\\{(?P<expr>.*?)\\}', re.DOTALL) def curly(s, frame = 2, source = None): def evalrepl(match, source = (source,)): f = sys._getframe(frame) if source is None: source = { } elif isinstance(source, dict): source = source elif hasattr(source, '__dict__'): source = source.__dict__ elif hasattr(source, '__slots__'): source = (dict,)((lambda .0: for x in .0: (x, getattr(source, x)))(source.__slots__)) else: raise AssertionError('not sure what to do with this argument: %r' % source) locals = dictadd(f.f_locals, source) try: res = eval(match.group('expr'), f.f_globals, locals) except Exception: e = None traceback.print_exc() return '' raise e return u'%s' % (res,) return _curlymatcher.sub(evalrepl, s) def cprint(s): print curly(s, frame = 3) def replace_newlines(s, replacement = ' / ', newlines = (u'\n', u'\r')): for newline in newlines[1:]: s = s.replace(newline, newlines[0]) while s.find(newlines[0] * 2) != -1: s = s.replace(newlines[0] * 2, newlines[0]) return s.strip().replace(newlines[0], replacement) replace_newlines = lru_cache(100)(replace_newlines) def nicenumber(n, sep = ',', decimal = '.'): (n, decimalpoint, decimalpart) = str(n).partition(decimal) n = str(n)[::-1] n = (sep.join,)((lambda .0: for i in .0: n[i:i + 3])(xrange(0, len(n), 3)))[::-1] return ''.join([ n, decimalpoint, decimalpart]) def nicebytecount(bytes): if bytes == 0: return '0B' count = 0 while math.log(bytes, 2) >= 10: bytes = bytes / 1024 count = count + 1 return (None if int(bytes) == bytes else '%.2f' + ' %sB') % (bytes, ('', 'k', 'M', 'G', 'T', 'P')[count]) class istr(unicode): def __new__(self, strng): return unicode.__new__(self, _(strng)) def __init__(self, strng): self.real = strng def __cmp__(self, other): if type(self) == type(other): return cmp(self.real, other.real) else: return unicode.__cmp__(self, other) def __eq__(self, other): return not bool(self.__cmp__(other)) def nicetimecount(seconds, max = 2, sep = ' '): seconds = int(seconds) if seconds < 0: return '-' + nicetimecount(abs(seconds), max = max, sep = sep) (minutes, seconds) = divmod(seconds, 60) (hours, minutes) = divmod(minutes, 60) (days, hours) = divmod(hours, 24) (years, days) = divmod(days, 365) i = 0 res = [] for thing in _('years days hours minutes seconds').split(): if not vars()[thing]: continue else: res.append('%d%s' % (vars()[thing], thing[0])) i += 1 if i == max: break continue elif not res: res = [ '0s'] return sep.join(res) def unpack_pstr(s): unpack = unpack import struct l = unpack('B', s[0])[0] return (s[1:1 + l], s[1 + l:]) def pack_pstr(s): pack = pack import struct return pack('B', len(s)) + s def preserve_newlines(s): return preserve_whitespace(s, nbsp = False) def preserve_whitespace(s, nbsp = True): s = s.replace('\r\n', '\n').replace('\n', '<br />') _re = re.compile('(\\s{2,})') if not nbsp: return s match = _re.search(s) while match: spaces = match.groups()[0] s = s.replace(spaces, len(spaces) * ' ', 1) match = _re.search(s) return s class EncodedString(object): def __init__(self, s, encodings = None): if encodings is None: encodings = () self.encodings = tuple(encodings) self._data = s def encode(self, encoding): return EncodedString(self._data.encode(encoding), self.encodings + (encoding,)) def decodeAll(self): s = self while s.encodings: s = s.decode() return s def decode(self): encoding = self.encodings[-1] newencodings = self.encodings[:-1] return EncodedString(self._data.decode(encoding), newencodings) def __getattr__(self, attr): if attr in ('encodings', '_data', 'encode', 'decode', 'decodeAll'): return object.__getattribute__(self, attr) else: return self._data.__getattribute__(attr) def __repr__(self): return '<%s (%s) encodings=%r>' % (type(self).__name__, repr(self._data), self.encodings) def __str__(self): if type(self._data) is str: return self._data else: raise TypeError('%r cannot be implicitly converted to str') def __unicode__(self): if type(self._data) is unicode: return self._data else: raise TypeError('%r cannot be implicitly converted to unicode') estring = EncodedString def try_all_encodings(s): successes = [] import encodings codecs = set(encodings.aliases.aliases.values()) for c in codecs: try: decoded = s.decode(c) except (Exception,): continue continue if isinstance(decoded, unicode): try: recoded = decoded.encode('utf8') except UnicodeEncodeError: continue except: None<EXCEPTION MATCH>UnicodeEncodeError None<EXCEPTION MATCH>UnicodeEncodeError if isinstance(recoded, str) and isinstance(decoded, unicode): codec = c successes.append((s, codec, decoded, recoded)) continue else: decoded = None recoded = None codec = None return successes def saferepr(obj): try: return repr(obj) except Exception: try: return '<%s>' % obj.__class__.__name__ except Exception: return '<??>' except: None<EXCEPTION MATCH>Exception None<EXCEPTION MATCH>Exception def wireshark_format(data): out = StringIO() safe = set(string.printable) - set(string.whitespace) | set(' ') def hex(s): return ' '.join((lambda .0: for ch in .0: '%02X' % ord(ch))(s)) def safe_bin(s): return (''.join,)((lambda .0: for ch in .0: None if ch in safe else '.')(s)) def pad(s, l, ch = ' '): return s + ch * (l - len(s)) w = out.write space = ' ' while data: chunk1 = data[:8] chunk2 = data[8:16] data = data[16:] w(pad(hex(chunk1), 24)) w(space) w(pad(hex(chunk2), 24)) w(space) w(pad(safe_bin(chunk1), 8)) w(space) w(pad(safe_bin(chunk2), 8)) w('\n') continue (None,) return out.getvalue() def to_hex(string, spacer = ' '): return spacer.join((lambda .0: for byte in .0: '%02X' % ord(byte))(string)) def byte_print(string_, spacer = ' '): import string as strng output = '' for i in range(len(string_) / 16 + 1): line = string_[i * 16:i * 16 + 16] pline = (''.join,)((lambda .0: for x in .0: None if x in strng.printable else '.')(line)) pline = pline.replace('\n', ' ') pline = pline.replace('\r', ' ') pline = pline.replace('\t', ' ') pline = pline.replace('\x0b', ' ') pline = pline.replace('\x0c', ' ') output += to_hex(line) + ' ' + pline + '\n' return output if __name__ == '__main__': import doctest doctest.testmod(verbose = True)